/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManVehiculo.java
 *
 * Created on 29/01/2011, 11:05:48 AM
 */
package com.catosist.run.application.vehiculo;

import com.catosist.run.application.Util.ButtonTabComponent;
import com.catosist.run.application.desktopapplication.DesktopApplicationView;
import com.catosist.run.application.vehiculo.documents.DocumentoVehiculo;
import com.catosist.run.application.vehiculo.gestion.GestionVehiculo;
import com.catosist.run.application.vehiculo.mantenimiento.ManMantenimiento;
import com.catosist.run.business.gestorDocumentosVehiculo.GestorDocumentosVehiculo;
import com.catosist.run.business.gestorMantenimiento.GestorMantenimiento;
import com.catosist.run.business.gestorcolores.GestorColores;
import com.catosist.run.business.gestormarca.GestorMarcas;
import com.catosist.run.business.gestorvehiculo.GestorVehiculo;
import java.awt.Component;
import javax.swing.JPanel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.swing.JOptionPane;

/**
 *
 * @author Carlos
 */
public class ManVehiculo extends javax.swing.JFrame {

    /** Creates new form ManVehiculo */
    public ManVehiculo() {
        initComponents();
        gestionVehiculo = new GestionVehiculo(gestorVehiculo);
    }

    public ManVehiculo(GestorVehiculo gestorVehiculo) {

        initComponents();
        this.gestorVehiculo = gestorVehiculo;
        gestionVehiculo = new GestionVehiculo(gestorVehiculo);

    }

    public ManVehiculo(DesktopApplicationView runv) {
//        this.viewRun = runv;
        initComponents();
        gestionVehiculo = new GestionVehiculo(gestorVehiculo);

//        refresh();
    }

    public ManVehiculo(boolean flag) {
        initComponents();
        iniciar();
        gestionVehiculo = new GestionVehiculo(gestorVehiculo);

//        refresh();
    }

    public ManVehiculo(GestorVehiculo gestorVehiculo, GestorColores gestorColores,
            GestorMarcas gestorMarcas) {
        this.gestorColores = gestorColores;
        this.gestorMarcas = gestorMarcas;
        this.gestorVehiculo = gestorVehiculo;
        initComponents();
        gestionVehiculo = new GestionVehiculo(gestorVehiculo);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        btnVerVehiculos = new javax.swing.JButton();
        btnAgregarVehi = new javax.swing.JButton();
        btnEditarVehi = new javax.swing.JButton();
        btnEliminarVehi = new javax.swing.JButton();
        btnBuscarVehi = new javax.swing.JButton();
        btnVerMantenimiento = new javax.swing.JButton();
        btnVerDocumentos = new javax.swing.JButton();
        tabWork = new javax.swing.JTabbedPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        btnVerVehiculos.setText("Ver Todos");
        btnVerVehiculos.setName("btnVerVehiculos"); // NOI18N
        btnVerVehiculos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerVehiculosActionPerformed(evt);
            }
        });

        btnAgregarVehi.setText("Nuevo");
        btnAgregarVehi.setName("btnAgregarVehi"); // NOI18N
        btnAgregarVehi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarVehiActionPerformed(evt);
            }
        });

        btnEditarVehi.setText("Editar seleccionado");
        btnEditarVehi.setName("btnEditarVehi"); // NOI18N
        btnEditarVehi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarVehiActionPerformed(evt);
            }
        });

        btnEliminarVehi.setText("Eliminar seleccionado");
        btnEliminarVehi.setName("btnEliminarVehi"); // NOI18N
        btnEliminarVehi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarVehiActionPerformed(evt);
            }
        });

        btnBuscarVehi.setText("Buscar");
        btnBuscarVehi.setName("btnBuscarVehi"); // NOI18N

        btnVerMantenimiento.setText("Mantenimientos");
        btnVerMantenimiento.setName("btnVerMantenimiento"); // NOI18N
        btnVerMantenimiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerMantenimientoActionPerformed(evt);
            }
        });

        btnVerDocumentos.setText("Documentos");
        btnVerDocumentos.setName("btnVerDocumentos"); // NOI18N
        btnVerDocumentos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerDocumentosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVerVehiculos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAgregarVehi)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditarVehi)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminarVehi)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnVerMantenimiento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnVerDocumentos, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnBuscarVehi)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnVerVehiculos)
                    .addComponent(btnAgregarVehi, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditarVehi)
                    .addComponent(btnEliminarVehi)
                    .addComponent(btnBuscarVehi)
                    .addComponent(btnVerMantenimiento)
                    .addComponent(btnVerDocumentos))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Gestion", jPanel2);

        tabWork.setName("tabWork"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 971, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabWork, javax.swing.GroupLayout.DEFAULT_SIZE, 969, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabWork, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVerVehiculosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerVehiculosActionPerformed
        // TODO add your handling code here:
        agregaPestanha(gestionVehiculo.getPanel(), "Ver todos los vehiculos");
    }//GEN-LAST:event_btnVerVehiculosActionPerformed

    private void btnAgregarVehiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarVehiActionPerformed
        // TODO add your handling code here:
        dialogVehi = new DialogVehi(this, true, 0, gestorVehiculo, gestorColores, gestorMarcas);
        dialogVehi.setLocationRelativeTo(this);
        dialogVehi.setVisible(true);
        gestionVehiculo.refresh();
    }//GEN-LAST:event_btnAgregarVehiActionPerformed

    private void btnEditarVehiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarVehiActionPerformed
        int id = gestionVehiculo.getSelectedId();
        dialogVehi = new DialogVehi(this, true, id, gestorVehiculo, gestorColores, gestorMarcas);
        dialogVehi.setLocationRelativeTo(this);
        dialogVehi.setVisible(true);
        gestionVehiculo.refresh();

    }//GEN-LAST:event_btnEditarVehiActionPerformed

    private void btnEliminarVehiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarVehiActionPerformed
        int id = gestionVehiculo.getSelectedId();
        int result = JOptionPane.showConfirmDialog(this, "Realmente desea eliminar el registro?", "Eliminar Vehiculo", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            gestorVehiculo.deleteById(id);
            gestionVehiculo.refresh();
        }
    }//GEN-LAST:event_btnEliminarVehiActionPerformed

    private void btnVerMantenimientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerMantenimientoActionPerformed
        // TODO add your handling code here:
        try {
            
            int i = gestionVehiculo.getSelectedId();
            
            manMantenimiento = new ManMantenimiento(i, gestorVehiculo);
            agregaPestanha(manMantenimiento.getPanel(), "Mantenimientos del vehiculo " + i);
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
            JOptionPane.showMessageDialog(this, "Debe seleccionar una vehiculo existente", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnVerMantenimientoActionPerformed

    private void btnVerDocumentosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerDocumentosActionPerformed
        // TODO add your handling code here:
        try{
            int i = gestionVehiculo.getSelectedId();
            if (i == 0) {
                throw new Exception();
            }
            documentoVehiculo = new DocumentoVehiculo(i);
            agregaPestanha(documentoVehiculo.getPanel(), "Documentos del vehiculo " + i);
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(this, "Debe seleccionar un vehiculo existente", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnVerDocumentosActionPerformed

    public void agregaPestanha(JPanel panel, String titulo) {
        this.tabWork.add(titulo, panel);
        this.tabWork.setSelectedIndex(this.tabWork.getTabCount() - 1);
        ButtonTabComponent pestana = new ButtonTabComponent("nano", this.tabWork);
        this.tabWork.setTabComponentAt(this.tabWork.getTabCount() - 1, pestana);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ManVehiculo(true).setVisible(true);
            }
        });
    }

    public JPanel getPanel() {
        return this.jPanel1;
    }

    private void iniciar() {
        ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        gestorVehiculo = (GestorVehiculo) ctx.getBean("gestorVehiculo");  //new Gesto
        //gestionVehiculo = new GestionVehiculo(gestorVehiculo);
//        gestorViaje.getAll();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarVehi;
    private javax.swing.JButton btnBuscarVehi;
    private javax.swing.JButton btnEditarVehi;
    private javax.swing.JButton btnEliminarVehi;
    private javax.swing.JButton btnVerDocumentos;
    private javax.swing.JButton btnVerMantenimiento;
    private javax.swing.JButton btnVerVehiculos;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane tabWork;
    // End of variables declaration//GEN-END:variables
    private GestorColores gestorColores;
    private GestorMarcas gestorMarcas;
    private GestorVehiculo gestorVehiculo;
    private GestionVehiculo gestionVehiculo;
    private ManMantenimiento manMantenimiento;
    private GestorMantenimiento gestorMantenimiento;
    private GestorDocumentosVehiculo gestorDocumentosVehiculo;
    private DocumentoVehiculo documentoVehiculo;
    ApplicationContext ctx;
    private DialogVehi dialogVehi;
}
